using System;
using System.Collections.Generic;
using System.Linq;
using Sustainalytics.Entities;
using Sustainalytics.Synchronization.Validation.Entities.CorporateGovernance;
using Sustainalytics.Synchronization.Validation.ValidationRules.Common;

namespace Sustainalytics.Synchronization.Validation.ValidationRules.CorporateGovernance
{
    public class GovernanceMissingOutlooksRule : Rule<GovernanceValidationProfile>
    {
        public override Severity Severity => Severity.Red;
        public override BusinessResearchProduct BusinessResearchProduct => BusinessResearchProduct.CorporateGovernance;
        public override string Code => "RR9";
        public override string InformationalMessage => "Outlooks for Section Overview G.1, G.2, G.3, G.4, G.5 Full Profile";

        public GovernanceMissingOutlooksRule()
        {
            For(x => x.GovernanceIssues).When(Validator).WithKey(x => x.CompanyId)
                .WithMessage("The company {0} #{1}# does not have outlook for Governance issues");
        }

        private bool Validator(GovernanceValidationProfile validatedEntity, GovernanceValidationProfile referenceEntity)
        {
            if (!validatedEntity.FullProfile)
                return true;

            var issues = (List<GovernanceIssue>)GetValue(validatedEntity);

            if (issues == null)
                return false;

            var triggerIssues = issues.Where(issue => 
                !string.Equals(issue.Number, "CG.0", StringComparison.InvariantCultureIgnoreCase) &&
                !string.Equals(issue.Number, "CG.6", StringComparison.InvariantCultureIgnoreCase) &&
                !issue.Outlook.HasValue)
                   .Select(x => x.Number).ToList();

            if (!triggerIssues.Any())
                return true;

            SetTriggersMessages(triggerIssues);

            return false;
        }
    }
}
